<include file="Public/header" title="{$siteSetting['site_seo_title']}" keywords="{$siteSetting['site_seo_keywords']}" description="{$siteSetting['site_seo_description']}" />

	<!-- footer -->
    <footer class="x-footer this-page" style="display: none;">
		<ul class="x-row x-footer-bar">
			<li class="x-col-6-12">
				<a href="javascript:;" class="button button-block button-highlight button-large" id="reScan">重新扫码</a>
			</li>
			<li class="x-col-6-12">
				<a href="javascript:;" class="button button-block button-primary button-large" id="btnAjaxSubmit">确定</a>
			</li>
		</ul>
	</footer>
	
	<!-- MAIN -->
	<section class="x-content x-m-b-50 this-page" style="display: none;">
		<!-- section -->
		<section class="x-box">
			<form id="form">
			<div class="x-box-body">
				<ul class="x-row x-list-input">
					<li class="x-col-12-12 with-border">
						<div class="text-center">
							<img alt="no pic" src="__TMPL__Public/assets/img/nopic128.png" id="bookpic" style="height: 200px;" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row x-relative">
							<label for="code" class="x-col-3-12 x-label-name">二维码<span class="text-red">*</span></label>
							<input type="text" class="x-col-9-12 x-input-value" name="code" id="code" placeholder="二维码" />
							<div class="x-line-right">
								<a href="javascript:;" class="button button-highlight button-rounded button-small button-p-10" id="btn-code">扫描二维码</a>
							</div>
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row x-relative">
							<label for="isbn" class="x-col-3-12 x-label-name">ISBN<span class="text-red">*</span></label>
							<input type="text" class="x-col-9-12 x-input-value" name="isbn" id="isbn" placeholder="ISBN" />
							<div class="x-line-right">
								<a href="javascript:;" class="button button-primary button-rounded button-small button-p-10" id="btn-isbn">扫描条形码</a>
							</div>
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="name" class="x-col-3-12 x-label-name">书名<span class="text-red">*</span></label>
							<input type="text" class="x-col-9-12 x-input-value" name="name" id="name" placeholder="书名" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="author" class="x-col-3-12 x-label-name">作者</label>
							<input type="text" class="x-col-9-12 x-input-value" name="author" id="author" placeholder="作者" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="translator" class="x-col-3-12 x-label-name">译者</label>
							<input type="text" class="x-col-9-12 x-input-value" name="translator" id="translator" placeholder="译者" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="pages" class="x-col-3-12 x-label-name">页数</label>
							<input type="text" class="x-col-9-12 x-input-value" name="pages" id="pages" placeholder="页数" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="price" class="x-col-3-12 x-label-name">价格</label>
							<input type="text" class="x-col-9-12 x-input-value" name="price" id="price" placeholder="价格" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="publisher" class="x-col-3-12 x-label-name">出版社</label>
							<input type="text" class="x-col-9-12 x-input-value" name="publisher" id="publisher" placeholder="出版社" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="pubdate" class="x-col-3-12 x-label-name">出版日期</label>
							<input type="text" class="x-col-9-12 x-input-value" name="pubdate" id="pubdate" placeholder="出版日期" />
						</div>
					</li>
					<li class="x-col-12-12 with-border">
						<div class="x-row">
							<label for="summary" class="x-col-3-12 x-label-name">简介</label>
							<textarea rows="6" class="x-col-9-12 x-input-value" name="summary" id="summary" placeholder="简介"></textarea>
						</div>
					</li>
				</ul>
			</div>
			
			<input type="hidden" name="thumb" id="thumb" />
			<input type="hidden" name="cid" id="cid" value="1" />
			<input type="hidden" name="cname" id="cname" value="魔力绘本馆" />
			<input type="hidden" name="lv" value="3" />
			</form>
		</section>
    </section>
	
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<link rel="stylesheet" href="__TMPL__Public/assets/plugins/xvalidate/xvalidate.css" />
<script src="__TMPL__Public/assets/plugins/xvalidate/xvalidate.js"></script>

<script>
$(function () {
    // 通过config接口注入权限验证配置
    wx.config({
	    debug: false, 							// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
		appId: '{$signPackage.appId}',			// 必填，公众号的唯一标识
		timestamp: '{$signPackage.timestamp}',	// 必填，生成签名的时间戳
		nonceStr: '{$signPackage.nonceStr}',	// 必填，生成签名的随机串
		signature: '{$signPackage.signature}',	// 必填，签名，见附录1
		jsApiList: ['scanQRCode']		// 必填，需要使用的JS接口列表，所有JS接口列表见附录2
	});

    // 通过ready接口处理成功验证，调起微信扫一扫接口扫描二维码并返回结果
	var xloading = xpopup.loading({content:'扫码系统初始化中...'});
    wx.ready(function() {
    	xloading.destroy();
    	$(".this-page").show();
    });
    
    wx.error(function(res) {
    	console.log(res);
    });
    
    // 点击扫描二维码
	$("#btn-code").click(function() {
    	wx.scanQRCode({
            needResult: 1,
            scanType: ["qrCode","barCode"],
            success: function (res) {
				$("#code").val(res.resultStr);
            }
		});
	});

	// 点击扫描ISBN
    $("#btn-isbn").click(function () {
    	wx.scanQRCode({
            needResult: 1,
            scanType: ["barCode"],
            success: function (res) {
				var tempArray = res.resultStr.split(',');							// 扫码结果是条形码，提取','后面的部分
				
				$.ajax({
					type: "get",
		            contentType: "application/json; charset=utf-8",
		            url: 'https://api.douban.com/v2/book/isbn/' + tempArray[1],  	// 这里是网址
		            dataType: "jsonp",												// 一定要确实正确
		            success: function (data) {
		            //	console.log(data);
		            	$("#isbn").val(data.isbn13);
		            	$("#name").val(data.title);
		            	
		            	var authors = '';
		            	for (var i = 0; i < data.author.length; i++) {
		            		authors += data.author[i] + ' ';
		            	}
		            	$("#author").val(authors);

		            	var translators = '';
		            	for (var i = 0; i < data.translator.length; i++) {
		            		translators += data.translator[i] + ' ';
		            	}
		            	$("#translator").val(translators);

		            	$("#pages").val(parseInt('0' + data.pages));		// number int				加0是为了保证当data.pages为空字符串时，parseInt不会出现NaN的情况
		            	$("#price").val(parseFloat('0' + data.price));		// number float
		            	$("#publisher").val(data.publisher);
		            	$("#pubdate").val(data.pubdate);
		            	$("#summary").val(data.summary);
		            	
		            	$("#bookpic").attr('src', data.images.large);
		            	$("#thumb").val(data.images.large);
		            },
		            error: function (XMLHttpRequest, textStatus, errorThrown) {
						if (textStatus == 'error') {
				           	xpop({
				               	title: '获取图书信息失败',
				                msg: '图书条形码读取异常！',
				                buttons: [{
				                    text: '确定',
				                    'class': 'weui_btn_dialog primary',
				                    handler: function ($this) {
				                		$this.destroy();
				                    }
				                }]
				            });
						}
		            }
		        });
            }
		});
	});
	
	$("#btnAjaxSubmit").click(function() {
		// validate
		var flag = true;
	    var requireArr = [{
			id: $("#code"),
			errmsg: "二维码不能为空"
		}, {
			id: $("#isbn"),
			errmsg: "ISBN不能为空"
		}, {
			id: $("#name"),
			errmsg: "书名不能为空"
		}];
	    $.each(requireArr, function(i, obj) {
	    	var flag_require = validate.require(obj.id, obj.errmsg);
	    	if (flag_require == false)
	    		flag = flag_require;
	    });
	    if (flag == false)
	    	return flag;
		
	    // submit
		$.ajax({
			type: 'post',
			url: "{:U('Book/Index/save')}",
	        contentType: 'application/x-www-form-urlencoded',
			data: $("#form").serialize(),
			dataType: 'json',
			async: false,
			success: function(data) {
			//	console.log(data);
				if (data.code == 0) {
					xpop.toast({
					    type: 'success',
				        msg: data.msg,
				        delay: 1000,
				        afterClose: function() {
				        	$("#form").find('input, textarea').val('');
				        	$("#bookpic").attr('src', '__TMPL__Public/assets/img/nopic128.png');
				            $('#toast').hide();
				        }
				    });
				} else {
		           	xpop({
		               	title: '添加失败',
		                msg: data.msg,
		                buttons: [{
		                    text: '确定',
		                    'class': 'weui_btn_dialog primary',
		                    handler: function ($this) {
		                		$this.destroy();
		                    }
		                }]
		            });
				}
			}
		});
	    
		return false;
	});

});
</script>
</body>
</html>